<?php
 class Categeory_model extends Model {
 	function Categeory_model()
 	{
 		parent::Model();
 	}
    // add categeory into database 
 	function addCategeory($catname,$parentid,$type="")
 	{
 		
  	  $this->db->insert('categeory',array('catname' =>$catname ,
 	  'parentid'=> $parentid));
 	
  	}
  	//getting info of categeories from database and saving 
  	//them in array options for dropdown list
  	function categeory($parentid)
	{
	  
	  $this->db->where('parentid',$parentid);
	  $this->db->order_by('id','desc');
	  $query = $this->db->get('categeory');
	  
	  $cat = $query->result();
      if($parentid ==0){
     $options[0] = 'Root';
      }
      else{
      $options[0] = 'Select';
      }
	  
	//sending root categeories to admin form
	  foreach($cat as $row)
        {
       	$options[$row->id]= $row->catname;
        }
        return $options;
   	}	
 	//extract all categery details for displaying

 	//extract all categery details for displaying   	
   function categeoryview($parentid,$num,$offset)
    {
		//extracting root categeories from database
		
		$this->db->where('parentid',$parentid);
		$this->db->order_by('id','desc');
		$query = $this->db->get('categeory',$num, $offset);
		return $query;
	}
	//checking whether root or not
	function isrootcategeory($id)
	{
		$this->db->where('id',$id);
		$query = $this->db->get('categeory');
		$result = $query->result();
		
	if($result[0]->parentid == 0)
	   return true;		
	else 
	   return false;
	   
	}
	function deletecategeory($id)
 	{
 		$this->db->where('id',$id);
 		$this->db->delete('categeory');
 		//if it is paent categeory delete sub categeories also.
 		
 		//redirect('admin/categeory/categeoryview/0');
  	}
  	function deletesubcategeory($id)
    {
 		$this->db->where('parentid',$id);
 		$this->db->delete('categeory');
 		//if it is paent categeory delete sub categeories also.
  	}
 	function cat($id)
  	{
  		$this->db->where('id',$id);
  		$this->db->order_by('id','desc');
 		$query = $this->db->get('categeory');
 		return $query->result();
  	}
 	//fetching all sub categeories from database
    function subcat($uriseg)
	{
		$this->db->where('parentid',$uriseg);
		$this->db->order_by('id','desc');
		$query =$this->db->get('categeory');
		return $query;
	}
	//editcategeory
 	function editcategeory($id,$catname)
 	{
 		$this->db->where('id',$id);
 		$this->db->update('categeory',array('catname' => $catname));
 		
 	}
    //extract image details from adatabase 
    function getImages($parentid)
    {
    	$this->db->where('parentid',$parentid);
    	$this->db->order_by('id','desc');
    	$this->db->select('id');
    	$query = $this->db->get('images');
    	return $query;
    }
    function image_view($parentid){
    	
    	$this->db->where('parentid',$parentid);
    	$this->db->order_by('id','desc');
    	$query = $this->db->get('images');
    	return $query;
    	
    }
    //edit image form
     function imageedit($uriseg)
     {
     	$this->db->where('id',$uriseg);
 		$query=$this->db->get('images');
 		return $query->result();
     	   	
     }
     //edit image
    function editimage($id,$title)
    {
    	$this->db->where('id',$id);
 		$this->db->update('images',array('title' => $title));
 	
    }
    //delete image
    function deleteimage($id)
 	{ 		
 		$this->db->where('id',$id);
 		$this->db->delete('images');
  	}
  	function deleteImages($keys)
  	{
  	//print_r($keys);
  	$this->db->query('delete from images where id in ('.$keys.')');
  		//$this->db->where_in('id',array(stripslashes($keys)));
  		//$this->db->delete('image');
  	}
  	
 }
 ?>